/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.qingtingshuzi.com
 * 注意：
 * 本软件为www.qingtingshuzi.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.qingting.common.util;


import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import co.qingting.utils.RedisUtils;
import co.qingting.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName 阿里云短信
 * @Author qingting <1010353475@qq.com>
 * @Date 2020/6/26
 **/
@Slf4j
@Configuration
public class SmsUtils {

    private static RedisUtils redisUtils;

    @Autowired
    public SmsUtils(RedisUtils redisUtils) {
        SmsUtils.redisUtils = redisUtils;
    }

    /**
     * 发送短信
     *
     * @param phoneNumbers  手机号
     * @param templateParam 短信模板变量对应的实际值，JSON格式
     */
    public static void sendSms(String phoneNumbers, String templateParam) throws ClientException {
        String regionId = redisUtils.getY("sms_region");
        String accessKeyId = redisUtils.getY("sms_access_key");
        String accessKeySecret = redisUtils.getY("sms_access_secret");
        String sign = redisUtils.getY("sms_sign");
        String templateId = redisUtils.getY("sms_templateId");
        DefaultProfile profile = DefaultProfile.getProfile(
                regionId,
                accessKeyId,
                accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();

        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", sign);
        request.putQueryParameter("TemplateCode", templateId);
        request.putQueryParameter("TemplateParam", templateParam);
        CommonResponse response = client.getCommonResponse(request);
        System.out.println(response.getData());
    }

    /**
     * 八米短信发送短信
     *
     * @param phoneNumbers  手机号
     * @param templateParam 短信模板变量对应的实际值，JSON格式
     */
    public static void sendSmsEightMeters(String phoneNumbers, String templateParam) throws ClientException {
        String eightMetersUrl = redisUtils.getY("eight_meters_url");
        String eightMetersUid = redisUtils.getY("eight_meters_uid");
        String eightMetersAccount = redisUtils.getY("eight_meters_account");
        String eightMetersPassword = redisUtils.getY("eight_meters_password");
        String eightMetersContent = redisUtils.getY("eight_meters_content");
        if (StringUtils.isNotEmpty(eightMetersContent)) {
            eightMetersContent = eightMetersContent.replace("888888", JSONObject.parseObject(templateParam).getString("code"));
        }
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userid", eightMetersUid);
        paramMap.put("account", eightMetersAccount);
        paramMap.put("password", eightMetersPassword);
//        paramMap.put("password", SecureUtil.md5(eightMetersPassword));
        paramMap.put("mobile", phoneNumbers);
        paramMap.put("content", eightMetersContent);
        paramMap.put("action", "send");
        paramMap.put("sendTime", null);
        paramMap.put("extno", null);
        String res = HttpUtil.post(eightMetersUrl, paramMap);
        System.out.println("============================================================================================验证码返回值="+res);
    }
}
